<?php
namespace Api\Controller;
use Think\Controller;
//角色权限功能
class RolesController extends CommonController{    


	//初始化角色权限
	public function set_baseinfo(){
		$detail = I('post.');
		$atype=$detail['atype'];
		$model = M('roles_menu');
		$roles = M("roles");
		$data=[];
		//业务员
		if($atype==1){
			$where['pid']=['in',[3,4]];
		}
		//财务
		if($atype==2){
			$where['pid']=75;
		}
		//采购
		if($atype==3){
			$where['pid']=60;
		}
		//仓库
		if($atype==4){
			$where['pid']=7;
		}
		//厂长
		if($atype==5){
			$where['pid']=7;
		}
		//委外工厂订单查看
		if($atype==7){
			$where['pid']=5;
			$where['id']=32;
		}
		//委外工厂外协订单查看
		if($atype==8){
			$where['pid']=5;
			$where['id']=33;
		}
		$list2=$model->where($where)->order('id asc')->field('id')->order('id asc')->select();
        if($list2){
			$at='1,';
            foreach($list2 as $k=>$val){
                   $at.=$val['id'].',';
			}
			$at1=trim($at,',');
			$data['action_list']=$at1;
		}
		$resault=$roles->where(['atype'=>$atype])->save($data);
		if($resault){
			$data_cc['code'] = 200;
			$data_cc['msg'] = "角色权限初始化成功";
			exit(json_encode($data_cc));
		}else{
			$data_cc['code'] = 0;
			$data_cc['msg'] = "角色权限初始化失败";
			exit(json_encode($data_cc));
		}
	}
	//添加权限到角色组
    public function adjsex(){
		$model = M('roles_menu');
		$roles = M("roles");
        $detail = I('post.');
		$js_id=$detail['js_id'];
		$qxat=$detail['qxat'];
		$qxatz=explode(',',$qxat);
		$xsa=count($qxat);
		if(empty($xsa)){
			$data_cc['code'] = 0;
			$data_cc['msg'] = "请选择角色权限";
			exit(json_encode($data_cc));
		}
		$where=[];
		$where['id']=$js_id;
		$info=$roles->where($where)->find();
        if($info){
			foreach($xsa as $vals){
				$cdrc=$model->wehre(['id'=>$vals])->count();
				if(empty($cdrc)){
						$data_cc['code'] = 0;
						$data_cc['msg'] = $vals."查询失败";
						exit(json_encode($data_cc));
				}
			} 
			$resault=$roles->where(['id'=>$js_id])->save(['action_list'=>$qxat]);
			if($resault){
				$data_cc['code'] = 200;
				$data_cc['msg'] = "角色权限分配成功";
				$data_cc['data']=$qxat;
				exit(json_encode($data_cc));
			}else{
				$data_cc['code'] = 0;
				$data_cc['msg'] = "角色权限分配失败";
				exit(json_encode($data_cc));
			}
		}else{
			$data_cc['code'] = 0;
			$data_cc['msg'] = "信息查询失败";
			exit(json_encode($data_cc));
		}

	}
	//查询账号个人权限信息
	public function admin_infoqx(){
		$detail = I('post.');
		$aId=$detail['aId'];
		$admin = M('admin');
		$model = M('roles_menu');
		$where['aId']=$aId;
		$admin_info=$admin->where($where)->find();
		if($admin_info){
				if($admin_info['aStatic']==0){
					$data_cc['code'] = 0;
					$data_cc['msg'] = "账号都冻结了还分配什么权限..";
					exit(json_encode($data_cc));
				}
				$dat=explode(',',$admin_info['qx_list']);
				$list=$model->where(['pid'=>0])->field('id,names,sort')->order('sort asc')->select();
				if($list){
					foreach($list as $k=>$val){
						if(in_array($val['id'],$dat)){
							$list[$k]['checked']="true";
						}
					$list2= $model->where(['pid'=>$val['id']])->field('id,names,sort')->order('sort asc')->select();
					if($list2){
						foreach($list2 as $k2=>$vals){
								if(in_array($vals['id'],$dat)){
									$list2[$k2]['checked']=true;
								}else{
									$list2[$k2]['checked']=false;
								}
						}
					} 
					$list[$k]['list2']=$list2;
					}
				}
				$data_cc['code'] = 200;
				$data_cc['msg'] = "查询成功";
				$data_cc['data']['qx_list'] =$admin_info['qx_list'];
				$data_cc['data']['list'] = $list;
				exit(json_encode($data_cc));
		}else{
			$data_cc['code'] = 0;
			$data_cc['msg'] = "信息查询失败";
			exit(json_encode($data_cc));
		}
	}
	//更新账号个人权限
	public function admin_updateqx(){
		$detail = I('post.');
		$aId=$detail['aId'];
		$qx_list=$detail['qx_list'];
		if(empty($qx_list)){
			$data_cc['code'] = 0;
			$data_cc['msg'] = "请选择权限进行提交";
			exit(json_encode($data_cc));
		}
		$csdt=explode(',',$qx_list);
		$dfgt=count($csdt);
		if($dfgt==0){
			$data_cc['code'] = 0;
			$data_cc['msg'] = "请选择权限进行提交1";
			exit(json_encode($data_cc));
		}
		$admin = M('admin');
		$where['aId']=$aId;
		$admin_info=$admin->where($where)->find();
		if($admin_info){
			if($admin_info['aStatic']==0){
				$data_cc['code'] = 0;
				$data_cc['msg'] = "账号都冻结了还分配什么权限..";
				exit(json_encode($data_cc));
			}
			$resault=$admin->where($where)->save(['qx_list'=>$qx_list]);
			if($resault){
				$data_cc['code'] = 200;
				$data_cc['msg'] = "权限编辑成功";
				exit(json_encode($data_cc));
			}else{
				$data_cc['code'] = 0;
				$data_cc['msg'] = "权限编辑失败";
				exit(json_encode($data_cc));
			}
		}else{
			$data_cc['code'] = 0;
			$data_cc['msg'] = "信息查询失败";
			exit(json_encode($data_cc));
		}
	}

	  //角色列表
      public function getjiaosqxn(){
		$detail = I('post.');
		$token=$detail['token'];
        $model = M('roles_menu');
        $roles = M('roles');
		$admin = M('admin');
		$token_model = M('token');
		$token_info=$token_model->where(['token'=>$token])->find();
		if(empty($token_info)){
			$data_cc['code'] = 0;
			$data_cc['msg'] = "token验证失败";
			exit(json_encode($data_cc));
		}

		$type=$token_info['type'];
		if($type==1){
			$aId=$token_info['aId'];
		}else{
			$data_cc['code'] = 0;
			$data_cc['msg'] = "用户登录后端权限不够...";
			exit(json_encode($data_cc));
		}
       	$admin_info=$admin->where(['aId'=>$aId])->field('atype,authority')->find();
		$authority=$admin_info['authority'];
        if($authority<>'ALL'){
			$info=$roles->where(['atype'=>$admin_info['atype']])->find();
			if(empty($info)){
				$data_cc['code'] = 0;
				$data_cc['msg'] = "角色权限验证失败";
				exit(json_encode($data_cc));
			}
			$action_list=$info['action_list'];
			if(empty($action_list)){
				$data_cc['code'] = 0;
				$data_cc['msg'] = "该角色还没配置权限的呢";
				exit(json_encode($data_cc));
			}
			$ac_lst=explode(',',$action_list);
			$list=$model->where(['pid'=>0])->field('id,names,sort')->order('sort asc')->select();
			if($list){
				foreach($list as $k=>$val){
					$list2= $model->where(['pid'=>$val['id']])->field('id,names,sort')->order('sort asc')->select();
					if($list2){
						foreach($list2 as $k2=>$vals){
								if(!in_array($vals['id'],$ac_lst)){
									unset($list2[$k2]);
								} 
						}
					}
					if($list2){
						$list[$k]['list2']=array_values($list2);
					}else{
						if($val['id']<>1){
							unset($list[$k]);
						}
					}
					
				}
				$list=array_values($list);

			}
		}else {
			$list= $this->getadx_cc();
		}


        $data_cc['code'] = 200;
		$data_cc['msg'] = "查询成功";
		$data_cc['data']['authority']=$authority;
		$data_cc['data']['qxlsit']=array_merge($list);
		exit(json_encode($data_cc)); 
    }
	    //超级管理员A
	function getadx_cc(){
			$roles = M('roles');
			$model = M('roles_menu');
			$list=$model->where(['pid'=>0])->field('id,names,sort')->order('sort asc')->select();
			if($list){
				foreach($list as $k=>$val){
				   $list2= $model->where(['pid'=>$val['id']])->field('id,names,sort')->order('sort asc')->select();
				   $list[$k]['list2']=$list2;
				}
			}
			return $list;
		}
		
	//角色列表
	public function roles_list(){
		$roles = M("roles");
		$where=[];
		$where['atype']=['neq',100];
		$where['id']=['not in',[13,14]];
		$fc_list= $roles->where($where)->field('id,names')->order('id asc')->select();
		$data_cc['code'] = 200;
        $data_cc['msg'] = "查询成功";
        $data_cc['data']['list']=$fc_list;
        exit(json_encode($data_cc));
	}
    //角色列表
    public function index_list(){
		$detail = I('post.');
		$where=[];
		$limit = $detail['limit']? : 10;
		$page = $detail['page']? : 1;
		$kewords=$detail['kewords'];
		if($kewords){
			$where['names']=array('like',"%".$kewords."%");
		}
        $roles = M("roles");
		$users_model = M("user");
		$admin_model = M("admin");
		$count=$roles->where($where)->count();
        $fc_list= $roles->where($where)
		->field('id,names,desct,atype,stype')
		->order('id asc')->page($page, $limit)->select();
		if($fc_list){
             foreach($fc_list as $k=>$val){
				 $stype=$val['stype'];
				 if($val['atype']==100){
						$tcount=$users_model->where(['types'=>$stype,'deletetime'=>0])->count(); 
				 }else{
					    $tcount=$admin_model->where(['atype'=>$val['atype']])->count(); 
				 }
				 $fc_list[$k]['total']=$tcount;
			 }
		}
        $data_cc['code'] = 200;
        $data_cc['msg'] = "查询成功";
        $data_cc['data']['count']=$count;
        $data_cc['data']['list']=$fc_list;
        exit(json_encode($data_cc));
    }
	public function detail(){
		$detail = I('post.');
		$id=$detail['id'];
		$limit = $detail['limit']? : 10;
		$page = $detail['page']? : 1;
		$kewords=$detail['kewords'];
		$roles = M("roles");
		$admin_model = M("admin");
		$users_model = M("user");
		$where=[];
		$where['id']=$id;
		$info=$roles->where($where)->find();
		if($info){
			$stype=$info['stype'];
			if($info['atype']==100){
				$t=1;
				$where2=[];
				$where2['types']=$stype;
				$where2['deletetime']=0;
				if($kewords){
					$where2['username|customer_no|mobile|names']=array('like',"%".$kewords."%");
				}
				$count=$users_model->where($where2)->count();
				$list=$users_model->where($where2)->field('uid,customer_no,username,mobile,names,avatar,sex,status,addtime')->page($page, $limit)->select(); 
				foreach($list as $k=>$val){
					if($val['aSex']==1){
						$sex='男';
					}else{
						$sex='女';
					}
					if($val['status']==1){
						$status='启用';
					}else{
						$status='冻结';
					}
					$list[$k]['sex']=$sex;
					$list[$k]['status_t']=$status;
			   }
			}else{
				$t=2;
				$where3=[];
				$where3['atype']=$info['atype'];
				if($kewords){
					$where3['aUser|aName|aTel']=array('like',"%".$kewords."%");
				}
				$count=$admin_model->where($where3)->count();
				$list=$admin_model->where($where3)->field('aId,aUser,aName,aTel,avatar,aSex,dept_name,aStatic,add_time')->page($page, $limit)->select();
				if($list){
                   foreach($list as $k=>$val){
						if($val['aSex']==1){
							$sex='男';
						}else{
							$sex='女';
						}
						if($val['aStatic']==1){
							$status='启用';
						}else{
							$status='冻结';
						}
                        $list[$k]['sex']=$sex;
						$list[$k]['status_t']=$status;
				   }
				} 
			}
			$data_cc['code'] = 200;
			$data_cc['msg'] = "查询成功";
			$data_cc['data']['t']=$t;
			$data_cc['data']['count']=$count;
			$data_cc['data']['list'] =$list;
			exit(json_encode($data_cc));
		}else{
			$data_cc['code'] = 0;
			$data_cc['msg'] = "信息查询失败";
			exit(json_encode($data_cc));
		}
	}

	//设置账号冻结和启用功能
	public function setint(){
		$detail = I('post.');
		$id=$detail['id'];
		$mtype=$detail['mtype'];
		$status=$detail['status'];
		$admin_model = M("admin");
		$users_model = M("user");
		if($mtype==1){
          $info=$admin_model->where(['aId'=>$id])->find();
          if($info){
			$resault=$admin_model->where(['aId'=>$id])->save(['aStatic'=>$status]);
			if($resault){
				$data_cc['code'] = 200;
				$data_cc['msg'] = "操作成功";
				$data_cc['status']=$status;
				exit(json_encode($data_cc));
			}else{
				$data_cc['code'] = 0;
				$data_cc['msg'] = "操作失败";
				exit(json_encode($data_cc));
			}
			
		  }else{
			$data_cc['code'] = 0;
			$data_cc['msg'] = "信息查询失败";
			exit(json_encode($data_cc));
		  } 
		}else{
            $info=$users_model->where(['uid'=>$id])->find();
			if($info){
				$resault=$users_model->where(['uid'=>$id])->save(['status'=>$status]);
				if($resault){
					$data_cc['code'] = 200;
					$data_cc['msg'] = "操作成功";
					$data_cc['status']=$status;
					exit(json_encode($data_cc));
				}else{
					$data_cc['code'] = 0;
					$data_cc['msg'] = "操作失败";
					exit(json_encode($data_cc));
				}
			}else{
				$data_cc['code'] = 0;
				$data_cc['msg'] = "信息查询失败";
				exit(json_encode($data_cc));
			} 
		}
	}

	//账号删除前后端
	public function del_mt(){
		$detail = I('post.');
		$id=$detail['id'];
		$mtype=$detail['mtype'];
		$admin_model = M("admin");
		$users_model = M("user");
		if($mtype==1){
			$info=$admin_model->where(['aId'=>$id])->find();
			if($info){
			  $resault=$admin_model->where(['aId'=>$id])->delete();
			  if($resault){
				  $data_cc['code'] = 200;
				  $data_cc['msg'] = "删除成功";
				  exit(json_encode($data_cc));
			  }else{
				  $data_cc['code'] = 0;
				  $data_cc['msg'] = "删除失败";
				  exit(json_encode($data_cc));
			  }
			  
			}else{
			  $data_cc['code'] = 0;
			  $data_cc['msg'] = "信息查询失败";
			  exit(json_encode($data_cc));
			  
			} 
		}else{
			$info=$users_model->where(['uid'=>$id])->find();
			if($info){
				$resault=$users_model->where(['uid'=>$id])->save(['deletetime'=>time()]);
				if($resault){
					$data_cc['code'] = 200;
					$data_cc['msg'] = "删除成功";
					exit(json_encode($data_cc));
				}else{
					$data_cc['code'] = 0;
					$data_cc['msg'] = "删除失败";
					exit(json_encode($data_cc));
				}
			}else{
				$data_cc['code'] = 0;
				$data_cc['msg'] = "信息查询失败";
				exit(json_encode($data_cc));
			} 
		}
	}
	//重置密码功能
	public function setpwd(){
		$detail = I('post.');
		$id=$detail['id'];
		$mtype=$detail['mtype'];
		$admin_model = M("admin");
		$users_model = M("user");
		$password='123456';
		if($mtype==1){
			$info=$admin_model->where(['aId'=>$id])->find();
			if($info){
			    $admin_model->where(['aId'=>$id])->save(['aPwd'=>md5($password)]);
				$data_cc['code'] = 200;
				$data_cc['msg'] = "密码初始化成功";
				exit(json_encode($data_cc));
			}else{
			  $data_cc['code'] = 0;
			  $data_cc['msg'] = "信息查询失败";
			  exit(json_encode($data_cc));
			} 
		}else{
			$info=$users_model->where(['uid'=>$id])->find();
			if($info){
				$salt=$info['salt'];
				$password_t=$this->getEncryptPassword($password,$salt);
				$users_model->where(['uid'=>$id])->save(['password'=>$password_t]);
				$data_cc['code'] = 200;
				$data_cc['msg'] = "密码初始化成功";
				exit(json_encode($data_cc));
			}else{
				$data_cc['code'] = 0;
				$data_cc['msg'] = "信息查询失败";
				exit(json_encode($data_cc));
			} 

		}
	}
	public function admin_list(){
		$detail = I('post.');
		$limit = $detail['limit']? : 10;
		$page = $detail['page']? : 1;
		$kewords=$detail['kewords'];
		$roles = M("roles");
		$admin_model = M("admin");
		$types=$detail['types'];
		$where['aId']=['NEQ',1];
		if($types<>'-1'){
			$atype=$roles->where(['id'=>$types])->getField('atype'); 
			$where['atype']=$atype;
		}
		if($kewords){
			$where['aUser|aName|aTel']=array('like',"%".$kewords."%");
		}
		$count=$admin_model->where($where)->count();
		$list=$admin_model->where($where)->field('aId,aUser,aName,aTel,avatar,aSex,dept_name,aStatic,add_time,atype')->page($page, $limit)->select();
		if($list){
			foreach($list as $k=>$val){
				 $list[$k]['jiaose_name']=$roles->where(['atype'=>$val['atype']])->getField('names'); 
				 if($val['aSex']==1){
					 $sex='男';
				 }else{
					 $sex='女';
				 }
				 if($val['aStatic']==1){
					 $status='启用';
				 }else{
					 $status='冻结';
				 }
				 $list[$k]['sex']=$sex;
				 $list[$k]['status_t']=$status;
			}
		} 
		$data_cc['code'] = 200;
		$data_cc['msg'] = "查询成功";
		$data_cc['data']['count']=$count;
		$data_cc['data']['list'] =$list;
		exit(json_encode($data_cc));
	}
	//会员列表
	public function user_list(){
		$detail = I('post.');
		$limit = $detail['limit']? : 10;
		$page = $detail['page']? : 1;
		$kewords=$detail['kewords'];
		$types=$detail['types'];
		$roles = M("roles");
		$user_model = M("user");
		$where=[];
		$where['deletetime']=0;
		if($kewords){
			$where['customer_no|username|mobile|names']=array('like',"%".$kewords."%");
		}
		if($types<>'-1'){
			$where['types']=$types;
		}
		$count=$user_model->where($where)->count();
		$list=$user_model->where($where)->field('uid,customer_no,username,mobile,names,sex,status,types,addtime')->page($page, $limit)->order('uid desc')->select();
		if($list){
			foreach($list as $k=>$val){
			 
				 if($val['sex']==1){
					 $sex='男';
				 }else{
					 $sex='女';
				 }
				 if($val['status']==1){
					 $status='正常';
				 }else{
					 $status='已冻结';
				 }
				 if($val['types']==0){
					$jiaose_type='客户';
				 } 
				 if($val['types']==1){
					$jiaose_type='员工';
				 } 
				 if($val['types']==2){
					$jiaose_type='组长';
				 } 
				 if($val['types']==3){
					$jiaose_type='质检员';
				 } 
				 $list[$k]['sext']=$sex;
				 $list[$k]['jiaose_type']=$jiaose_type;
				 $list[$k]['status_t']=$status;
			}
		} 
		$data_cc['code'] = 200;
		$data_cc['msg'] = "查询成功";
		$data_cc['data']['count']=$count;
		$data_cc['data']['list'] =$list;
		exit(json_encode($data_cc));
	}
	//查看会员详情接口
	public function user_detail(){
		$detail = I('post.');
		$id=$detail['id'];
		$where=[];
		$where['uid']=$id;
		$where['deletetime']=0;
		$user_model = M("user");
		$info=$user_model->where($where)->find();
		if($info){
			unset($info['password']);
			if($info['types']>0){
				unset($info['company']);
				unset($info['credit']);
				unset($amount['password']);
				unset($amount['tin']);
				unset($amount['salt']);
				unset($amount['yw_id']);
				unset($amount['customer_attr']);
				unset($amount['yw_id']);
			}
			$info['sh_text']='待审核';
			$info['gl_text']='否';
			$info['sta_text']='冻结';
			if($info['issh']==1){
				$info['sh_text']='已审核';
			}
			if($info['isgl']==1){
				$info['gl_text']='是';
			}
			if($info['status']==1){
				$info['sta_text']='正常';
			}
			$data_cc['code'] = 200;
			$data_cc['msg'] = "查询成功";
			$data_cc['data']['info'] =$info;
			exit(json_encode($data_cc));
		}else{
				$data_cc['code'] = 0;
				$data_cc['msg'] = "信息查询失败";
				exit(json_encode($data_cc));
		}
	}
	//编辑更新用户账号
	public function edit_user(){
		$detail = I('post.');
		$uid=$detail['uid'];
		$where=[];
		$where['uid']=$uid;
		$where['deletetime']=0;
		$user_model = M("user");
		$info=$user_model->where($where)->find();
		if($detail['mobile']){
			$where2['uid']=['neq',$uid];
			$where2['mobile']=trim($detail['mobile']);
			$sder=$user_model->where($where2)->find();
			if($sder){
				$data_cc['code'] = 0;
				$data_cc['msg'] = "该手机号已被注册过了";
				exit(json_encode($data_cc));
			}
		}
		$data['mobile']=$detail['mobile'];
		$data['names']=$detail['names'];
		$data['sex']=$detail['sex'];
        if($detail['avatar']){
			$data['avatar']=$detail['avatar'];
		}
		$password=$detail['password'];
		if($info){	    
			if($password){
				$salt=$info['salt'];
				$password_t=$this->getEncryptPassword($password,$salt);
				$data['password']=$password_t;
			}
			$resault=$user_model->where(['uid'=>$uid])->save($data);
			if($resault){
				$data_cc['code'] = 200;
				$data_cc['msg'] = "更新成功";
				exit(json_encode($data_cc));
			}else{
				$data_cc['code'] = 0;
				$data_cc['msg'] = "更新失败";
				exit(json_encode($data_cc));
			}	
		}else{
			$data_cc['code'] = 0;
			$data_cc['msg'] = "信息查询失败";
			exit(json_encode($data_cc));
		}
	}
	//更新权限说明功能
	public function edit_roles(){
		$detail = I('post.');
		$id=$detail['id'];
		$desct=$detail['desct'];
		$roles = M("roles");
		$where=[];
		$where['id']=$id;
		$info=$roles->where($where)->find();
		if($info){	    
			$resault=$roles->where(['id'=>$id])->save(['desct'=>$desct]);
			if($resault){
				$data_cc['code'] = 200;
				$data_cc['msg'] = "更新成功";
				exit(json_encode($data_cc));
			}else{
				$data_cc['code'] = 0;
				$data_cc['msg'] = "更新失败";
				exit(json_encode($data_cc));
			}	
		}else{
			$data_cc['code'] = 0;
			$data_cc['msg'] = "信息查询失败";
			exit(json_encode($data_cc));
		}
		
	}
		//校色权限详情
		public function detail_roles(){
			$detail = I('post.');
			$id=$detail['id'];
			$model = M('roles_menu');
			$roles = M("roles");
			$where=[];
			$where['id']=$id;
			$info=$roles->where($where)->find();
			if($info){	
				$dat=explode(',',$info['action_list']);
				$list=$model->where(['pid'=>0])->field('id,names,sort')->order('sort asc')->select();
				if($list){
					foreach($list as $k=>$val){
						if(in_array($val['id'],$dat)){
							$list[$k]['checked']="true";
						}
					   $list2= $model->where(['pid'=>$val['id']])->field('id,names,sort')->order('sort asc')->select();
					   if($list2){
                          foreach($list2 as $k2=>$vals){
							     if(in_array($vals['id'],$dat)){
									$list2[$k2]['checked']=true;
								 }else{
									$list2[$k2]['checked']=false;
								 }
						  }
					   } 
					   $list[$k]['list2']=$list2;
					}
				}
				$data_cc['code'] = 200;
				$data_cc['msg'] = "查询成功";
				$data_cc['data']['info']=$info['desct'];
				$data_cc['data']['action_list']=$info['action_list'];
				$data_cc['data']['list']=$list;
				exit(json_encode($data_cc));
			}else{
				$data_cc['code'] = 0;
				$data_cc['msg'] = "信息查询失败";
				exit(json_encode($data_cc));
			}    
		}

	//添加质检员
	public function add_zhjianuser(){
		$detail = I('post.');
		$data=[];
		$user = D("User");
		$username=trim($detail["username"]);
		$mobile = $detail['mobile'];
		$password = $detail['password'];
		$user_num = $user->where(['username' => $username])->count();
		$mobile_num = $user->where(['mobile' => $mobile])->count();
		$names = $detail['names'];
		$avatar=$post['avatar'];
		if (empty($username)) {
			$data_cc['code'] = 0;
			$data_cc['msg'] = "登录账号不能为空！";
			exit(json_encode($data_cc));
        }
        if ($user_num > 0) {
			$data_cc['code'] = 0;
			$data_cc['msg'] = "该账号已被注册！请重新输入！";
			exit(json_encode($data_cc));
        }
        if (empty($mobile)) {
			$data_cc['code'] = 0;
			$data_cc['msg'] = "手机号码不能为空！";
			exit(json_encode($data_cc));
        }
        if ($mobile_num > 0) {
			$data_cc['code'] = 0;
			$data_cc['msg'] = "该手机号已被注册！请重新输入！";
			exit(json_encode($data_cc));
        }
        if (empty($password)) {
			$data_cc['code'] = 0;
			$data_cc['msg'] = "密码不能为空！";
			exit(json_encode($data_cc));
        }
		
        if (empty($names)) {
			$data_cc['code'] = 0;
			$data_cc['msg'] = "客户姓名不能为空！";
			exit(json_encode($data_cc));
        }
		$num = $user->where(['types' =>3])->count();
		$salt = $this->GetRandStr(6);
		$fdh['password'] = $this->getEncryptPassword($password, $salt);
		$fdh['salt'] = $salt;
		$fdh['customer_no'] = $this->getsn('ZJBH');
		$fdh['username'] =$username ;
		$fdh['mobile'] = $detail["mobile"];
		$fdh['names'] = $detail["names"];
		$fdh['sex'] = $detail["sex"];
		$fdh['status'] = 1;
		$fdh['types'] = 3;
		$fdh['addtime'] = date('Y-m-d H:i:s');
		if(empty($avatar)) {
            $fdh['avatar'] = 'https://wages-zymaimai.oss-cn-beijing.aliyuncs.com/2023/03/02/640060c849864.jpg';
        } else {
            $fdh['avatar'] = $avatar;
        }
		$result = $user->add($fdh);
		if($result){
			$data_cc['code'] = 200;
			$data_cc['msg'] = "注册成功";
			exit(json_encode($data_cc));
		}else{
			$data_cc['code'] = 0;
			$data_cc['msg'] = "注册失败";
			exit(json_encode($data_cc));
		}
	}	
	 // 密码加密
	 function getEncryptPassword($password, $salt = ''){
		 return md5(md5($password) . $salt);
	 }
	// 密码验证参数
	function GetRandStr($length){
		$str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890';
		$randStr = str_shuffle($str); //打乱字符串
		$rands = substr($randStr, 0, $length); //substr(string,start,length);返回字符串的一部分
		return $rands;
	}
	
}
